<?php

class type_plain{
	
	public $id;
	public $title;
	
	protected $old;
	
	/**
	 * constructor
	 * 
	 * @author	Omid Mottaghi
	 * @return 	
	 */
	function __construct(){
		global $lb;
	
		$this->id 	= $lb['page']->type_id;
		$this->title= $this->get_title();
	}

	/**
	 * generate and return title of page
	 * 
	 * @author	Omid Mottaghi
	 * @return 	string		page's title on success or false on failure
	 */
	function get_title(){
		global $lb;
		
		$params = array (
			array ($this->id, 'int')
		);
			
		$result = $lb['db']->query('SELECT title FROM #__type_plain
									WHERE id=#', $params);
		
		// if page exist
		if($result['num_of_rows'] > 0){
			return $result['rows'][0]['title'];
		}else{
			return false;
		}
	}
	
	/**
	 * generate and return content of page
	 * 
	 * @author	Omid Mottaghi
	 * @param 	boolean		if current user can write this page, it will be true
	 * @return 	string		page's content
	 */
	function get_content($freez=true){
		global $lb;
		
		$params = array (
			array ($this->id, 'int')
		);
		
		$result = $lb['db']->query('SELECT body FROM #__type_plain
									WHERE id=#', $params);
		
		// if page exist
		if($result['num_of_rows'] > 0){
			if($freez){
				$result = lb_page::wikicode($result['rows'][0]['body']);
			}else{
				$result = stripslashes($result['rows'][0]['body']);
			}
			return $result;
		}else{
			return false;
		}
	}

	/**
	 * update edited page information in database
	 * 
	 * @author	Omid Mottaghi
	 * @author	Armin Borumand
	 * @param	int		page id
	 * @param	array		title=>page title, content=>page contents
	 * @return 	boolean	true (or page's content) on success or false on failure
	 */
	function edit_page(&$arr){
		global $lb;
		
		if($arr['title'] == ''){
			return false;
		}
		$arr['content'] = type_plain::html_filter(&$arr['content']);
		
		$this->old['title']		= $this->title;
		$this->old['content']	= $this->get_content(false);
		
		$params = array (
			array ($arr['title']	,'text'	),
			array ($arr['content']	,'text' ),
			array ($this->id,'int'	)
		);
		
		$result = $lb['db']->query('UPDATE #__type_plain
									SET title=#,
										body=#
									WHERE id=#', $params);
		
		if($result['num_of_rows'] < 0){
			return false;
		}else{
			$this->title = $arr['title'];
			return $this->id;
		}
	}
	
	/**
	* Restore old title and content in plugin database
	*
	* @author	Armin borumand
	* @return	boolean	true on success and false on failure
	*/
	function restore($add=false){
		global $lb;

		if($add === false){
			$result = $this->edit_page($this->old);
		}else{
			$result = $this->add_page($this->old, $this->id);
		}
		
		if($result){
			$arr['title'] = $this->old['title'];
			$this->old = array();
			return true;
		}else{
			return false;
		}
	}
	/**
	 * install this page type
	 * 
	 * @author	Omid Mottaghi
	 * @author	Armin Borumand
	 * @return	boolean		return true if successfull installed
	 */
	function install(){
		global $lb;
		
		$query = '
			CREATE TABLE `#__type_plain` (
			  `id` int(11) NOT NULL auto_increment,
			  `title` varchar(255) default NULL,
			  `body` mediumtext,
			  PRIMARY KEY  (`id`)
			) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ';
		
		$result = $lb['db']->query($query, array());
		
		return $result['result'];
		
	}
	
	/**
	 * uninstall this page type
	 * 
	 * @author	Omid Mottaghi
	 * @author	Armin Borumand
	 * @return	boolean		return true if successfull uninstalled
	 */
	function uninstall(){
		global $lb;
		
		$query = 'DROP TABLE `#__type_plain`';
		
		$result = $lb['db']->query($query, array());
		
		return $result['result'];
	}
	

	/**
	* Add a new page in type_plain database
	*
	* @author	Armin Borumand
	* @param	array		title=>page title, content=>page content
	* @param	boolean	remove
	* @return added page id(type_plain::id)
	*/
	function add_page(&$arr, $id=false){
		global $lb;
		
		if($arr['title'] == ''){
			return false;
		}
		$arr['content'] = type_plain::html_filter($arr['content']);
		
		$query = 'INSERT INTO #__type_plain';
		
		$params = array (
			array ($arr['title'],	'text'),
			array ($arr['content'],'text')
		);
		
		if($id == false){
			$query .= '(title, body) VALUES (#, #)';
		}else{
			$query .= '(id, title, body) VALUES (#, #, #)';
			$params = array_unshift($params, array($id, 'int'));
		}
						
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] < 1){
			return false;
		}
		
		return $result['insert_id'];
	}
	
	/**
	* Remove this page from plain database
	*
	* @author	Armin Borumand
	* @param	int		page type id to be removed (if false remove current page)	
	* @return	boolean	true on success and false on failure
	*/
	function remove_page($id = false){
		global $lb;
		if($id == false){
			$id = $this->id;
		}
		
		$params = array (
			array ($id , 'int')
		);
		
		$query = 'DELETE FROM #__type_plain
						WHERE id = #';
		
		$result = $lb['db']->query($query, $params);
	
		if($result['num_of_rows'] == 1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	* search in pages, store search result in self::$search_archive
	* future: we must use a database for storing search result and refere to search result by an individual id
	*
	* @author	Armin Borumand
	* @param	array		search words
	* @param	int		limit start row
	* @param	int		number of results
	* @return	array		consist of page title and ids
	*/
	function search(&$arr, $start, $number){
		global $lb;
		
		
		$conds = '';
		foreach($arr as $val){
			$params[] = array('%'.$val[1].'%', 'text');
			$conds .= 'CONCAT(title, body) like # '.$val[0].' ';
		}			
		$conds = substr($conds, 0, strlen($conds)-3);
			
		//Setting LIMIT ranges
		$params[] = array($start,	'int');
		$params[] = array($number,	'int');
		
		//double use!
		$params = array_merge($params, $params);
		array_pop($params);
		array_pop($params);
		
		$result = $lb['db']->query('SELECT id ,title 
						FROM #__type_plain 
							WHERE ' . $conds . 'LIMIT #,# 
						UNION SELECT count(id), NULL
						FROM #__type_plain 
							WHERE ' . $conds, $params);
		if($result['num_of_rows'] < 1){
			$result['rows'] = array();
		}
		
		$num = $result['num_of_rows'] -1 ;
		if($num < $number){
			$number = $num;
		}
		
		$result['num_of_rows'] = intval($result['rows'][$number]['id']);
		array_pop($result['rows']);			
			
		if($num == 0){
			return $result;
		}
			
		for($i = 0; $i < $num; $i++){
			$plain_ids[] = $result['rows'][$i]['id'];
		}
			
		$page_ids = lb_page::get_id($plain_ids, 'plain');
						
		for($i = 0; $i < $num; $i++){
			$result['rows'][$i]['id'] = $page_ids[$plain_ids[$i]];
		}	

		
		return $result;
	}
	
	/**
	 * Filter input text from html tags
	 *
	 * @access	protected
	 * @param	string	input text
	 * @param	string	clean text
	 */
	function html_filter($string){
		//html allowable tags
		$allowable = array('<br>', '<a>', '<p>', '<div>', '<b>', '<strike>', '<u>', '<em>', '<strong>'
					     , '<blockquote>', '<sup>', '<sub>', '<ul>', '<ol>', '<li>', '<table>', '<tr>', '<td>'
						 , '<tbody>', '<img>');
		return lb_system::strip_tags($string, $allowable);
	}
	/**
	* Return number of pages
	*
	* @author	Armin Borumand
	* @return	int	number of pages
	*/
	function count(){
		global $lb;
		
		$result = $lb['db']->query('SELECT count(*) AS count FROM #__type_plain', array());
		
		if($result['num_of_rows'] > 0){
			return $result['rows'][0]['count'];
		}else{
			return -1;
		}
	}

}

?>